package com.icefish.control;

import com.alibaba.fastjson.JSONObject;
import com.icefish.bean.Order;
import com.icefish.bean.fastjsonParse.Meta;
import com.icefish.bean.fastjsonParse.settlement.SettlementResp;
import com.icefish.bean.fastjsonParse.settlement.SettlementReturnInfo;
import com.icefish.dao.OrderDao;
import com.icefish.dao.UserDao;
import com.icefish.daoFactory.DaoFactory;
import com.icefish.utils.Init_Req_Resp_Config;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Date;

@WebServlet("/settlement")
public class settlement extends HttpServlet {

    private OrderDao orderDao = DaoFactory.getOrderDaoInstance();
    private UserDao userDao = DaoFactory.getUserDaoInstance();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // 标准配置
        Init_Req_Resp_Config.init(request, response);

        Boolean flag =true;
        SettlementResp settlementResp = new SettlementResp();

        //获取请求参数
      /*  String orderTime = request.getParameter("order_time");*/
        //转换时间格式

//        Date date = DateTransform.stringToDate(orderTime);
        Date date = new Date();

        Integer orderCount=Integer.valueOf(request.getParameter("order_count"));
        Integer orderAmount=Integer.valueOf(request.getParameter("order_amount"));
        Integer userID=Integer.valueOf(request.getParameter("user_id"));
        String username=request.getParameter("username");
        Integer goodId= Integer.valueOf(request.getParameter("goods_id"));
        String goodName = request.getParameter("goods_name");
        Integer goodPrice = Integer.valueOf(request.getParameter("goods_price"));
        String goodPicture = request.getParameter("goods_picture");
        String goodDescribe = request.getParameter("goods_describe");

        //创建Order对象接收参数
        Order order = new Order();

        order.setOrder_time(date);
        order.setOrder_count(orderCount);
        order.setOrder_amount(orderAmount);
        order.setUser_id(userID);
        order.setUsername(username);
        order.setGoods_id(goodId);
        order.setGoods_name(goodName);
        order.setGoods_price(goodPrice);
        order.setGoods_picture(goodPicture);
        order.setGoods_describe(goodDescribe);

        try {
            //将order传给dao执行并返回所执行成功的语句数
            Integer i = orderDao.insertCartToOder(order);

            //判断是否添加成功
            if(i!=null)
                flag=true;

        } catch (Exception e) {
            e.printStackTrace();
        }
        if (flag==true){
            //对settlementResp数据部分赋值
            SettlementReturnInfo settlementReturnInfo = new SettlementReturnInfo();
            settlementReturnInfo.setThanks("没有别的意思，谢谢老板");
            settlementResp.setData(settlementReturnInfo);
            //对settlementResp Meta部分赋值
            Meta meta = new Meta("结算商品成功",201);
            settlementResp.setMeta(meta);
        }
        else {
            Meta meta = new Meta("结算商品失败",201);
        }

        //根据用户Id和订单总金额修改用户表的余额
        try {
            //获取用户余额
            Integer wallet = userDao.selectWalletByUserId(order.getUser_id());

            System.out.println("========="+wallet+"==================");

            //用户余额减去订单总金额得剩余金额
            wallet-= order.getOrder_amount();

            //根据用户ID和余额修改用户表余额
            userDao.updateUserWallet(order.getUser_id(),wallet);

        } catch (SQLException e) {
            e.printStackTrace();
        }


        //相应Json数据
        JSONObject jsonObject = new JSONObject();
        // 转换成json
        String backToBrowser = jsonObject.toJSONString(settlementResp);

        // 响应数据
        response.getWriter().write(backToBrowser);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
